<?php
/* ******************************
 *	Partner aJax actions
 * ******************************
 */

include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error 		= '';
$data 		= '';

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $part_id = $_REQUEST['id'];
		$page		= GetPage(GetPartner($part_id));
        
        $data 		= array('page'	=> $page);
        
        break;
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    
		$rResult = mysql_query("SELECT `partniorebi`.`id`,
                                       `partniorebi`.`partn_daaxeleba`,
                                       `partniorebi`.`partn_id_kodi`,
                                       `partniorebi`.`partn_fiz_misamarti`,
                                       `partniorebi`.`partn_org_telefoni`,
                                       `partner_contact_info`.`l_name`,
									   `pay_method`.`name`
							    FROM `partniorebi` 
							    	LEFT JOIN `partner_contact_info`
									ON `partniorebi`.`id` = `partner_contact_info`.`partner_id` 
									LEFT JOIN `pay_method`
									ON `partniorebi`.`gadaxdis_met` = `pay_method`.`id`
								GROUP BY `partniorebi`.`partn_daaxeleba`
								ORDER BY `partniorebi`.`id`");
		
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'save_part':
		$part_id 		= $_REQUEST['id'];
		
		$p_user_id 		= $_REQUEST['pui'];
		$p_name 		= $_REQUEST['pn'];
		$pu_address		= $_REQUEST['pua'];
		$pp_address		= $_REQUEST['ppa'];
		$p_data 		= $_REQUEST['pd'];
		$ps_number 		= $_REQUEST['psn'];
		
		$payer	 		= $_REQUEST['py'];
		$p_status 		= $_REQUEST['ps'];
		$c_form 		= $_REQUEST['cf'];
		
		if($p_user_id != '' && $part_id == ''){
			if(!CheckPartnerExist($p_user_id)){			
				$in_id = AddPartner( $user_id, $p_user_id, $p_name, $pu_address, $pp_address, $p_data, $ps_number, $payer, $p_status, $c_form);
				$data = array('in_id' => $in_id);
			} else {
				$error = '"' . $p_user_id . '" უკვე არის სიაში!';
			}
		}else{
			SavePartner($user_id, $part_id, $p_name, $pu_address, $pp_address, $p_data, $ps_number, $payer, $p_status, $c_form);
		}
		
        break;
    case 'disable':
		$part_id 		= $_REQUEST['id'];
		DeletePartner($part_id);
		
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Partner Functions
 * ******************************
 */

function AddPartner($user_id, $p_user_id, $p_name, $pu_address, $pp_address, $p_data, $ps_number, $payer, $p_status, $c_form)
{
	mysql_query("INSERT INTO `partniorebi`
					(`user_id`, `partn_daaxeleba`, `part_iur_statusi`, `partn_id_kodi`, `partn_iur_misamarti`, `partn_fiz_misamarti`, `partn_VAT_sertifikati`, `gadaxdis_met`, `vat_data`, `vat_num`) 
				 VALUES
					('$user_id', '$p_name', $p_status, '$p_user_id', '$pu_address', '$pp_address', $payer, $c_form, '$p_data', '$ps_number')");
					
	return mysql_insert_id();
}

function CheckPartnerExist($part_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`
										  FROM `partniorebi`
										  WHERE `partn_id_kodi` = '$part_id'"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}

function SavePartner($user_id, $part_id, $p_name, $pu_address, $pp_address, $p_data, $ps_number, $payer, $p_status, $c_form)
{
	mysql_query("UPDATE
	    			`partniorebi`
				 SET
				 	`user_id`= '$user_id',					    
				    `partn_daaxeleba` = '$p_name',
				    `part_iur_statusi` = $p_status,
				    `partn_iur_misamarti` = '$pu_address',
				    `partn_fiz_misamarti` = '$pp_address',
				    `partn_VAT_sertifikati` = $payer,
				    `gadaxdis_met` = $c_form,
				    `vat_data` = '$p_data',
				    `vat_num` = '$ps_number'
				 WHERE
					`id` = $part_id");
}

function DeletePartner($part_id)
{
    mysql_query("DELETE 
				 FROM
	    			`partniorebi`
				 WHERE
					`id` = $part_id ");
					
    mysql_query("DELETE 
				 FROM
	    			`partner_bank`
				 WHERE
					`partn_id` = $part_id ");
					
    mysql_query("DELETE 
				 FROM
	    			`partner_contact_info`
				 WHERE
					`partner_id` = $part_id ");
}

function GStatus($point)
{
	$data = '';
    $req = mysql_query("SELECT `id`, `name`
							FROM `legal_status`");
										
	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}
		
	while( $res = mysql_fetch_assoc($req)){			
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}				
	}
		
	return $data;
}

function PayForm($point)
{
	$data = '';
    $req = mysql_query("SELECT `id`, `name`
							FROM `pay_method`");
	
	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}
	
	while( $res = mysql_fetch_assoc($req)){			
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}				
	}
	
	return $data;
}

function VatStatus($point)
{
	$data = '';
	
	switch ($point) {
	    case 0:
			$data = '<option value="1">კი</option>
					 <option value="0" selected="selected">არა</option>';
	        break;
	    case 1:
			$data = '<option value="1" selected="selected">კი</option>
					 <option value="0">არა</option>';
	        break;
	    default:
			$data = '<option value="1">კი</option>
					 <option value="0" selected="selected">არა</option>';
	}
	
	return $data;
}

function GetPartner($part_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`,
    											 `partn_id_kodi`,
											     `part_iur_statusi`,
    											 `partn_daaxeleba`,
											     `partn_iur_misamarti`,
											     `partn_fiz_misamarti`,
											   	 `gadaxdis_met`,
											   	 `partn_VAT_sertifikati`,
											   	 `vat_data`,
											   	 `vat_num`
									      FROM `partniorebi`
									      WHERE `id` = $part_id"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">		    
		<div id="tabs">
			<ul>
			    <li><a href="#tabs-1">ძირითადი ინფორმაცია</a></li>
			    <li><a href="#tabs-2">ანგარიშწორება</a></li>
			    <li><a href="#tabs-3">საბანკო რეკვიზიტები</a></li>
			    <li><a href="#tabs-4">კადრები</a></li>
			</ul>
			<!-- Main Info -->
			<div id="tabs-1">
				<fieldset>
					<legend>პარტნიორი</legend>
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 170px;"><label for="p_user_id">საიდ. კოდი/ პირადი №</label></td>
							<td>
								<input type="text" id="p_user_id" class="idle user_id" onblur="this.className=\'idle user_id\'" onfocus="this.className=\'activeField user_id\'" value="' . $res['partn_id_kodi'] . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="p_status">იურ. სტატუსი</label></td>
							<td>
								<select id="p_status" class="idls">' . GStatus($res[part_iur_statusi]) . '</select>
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="p_name">დასახელება</label></td>
							<td>
								<input type="text" id="p_name" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['partn_daaxeleba'] . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="pu_address">იურიდიული მისამართი</label></td>
							<td>
								<input type="text" id="pu_address" class="idle address" onblur="this.className=\'idle address\'" onfocus="this.className=\'activeField address\'" value="' . $res['partn_iur_misamarti'] . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="pp_address">ფაქტიური მისამართი</label></td>
							<td>
								<input type="text" id="pp_address" class="idle address" onblur="this.className=\'idle address\'" onfocus="this.className=\'activeField address\'" value="' . $res['partn_fiz_misamarti'] . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="c_form">ანგარიშსწორების ფორმა</label></td>
							<td>
								<select id="c_form" class="idls">' . PayForm($res[gadaxdis_met]) . '</select>
							</td>
						</tr>
					</table>
			    </fieldset>
			    <fieldset>
			    	<legend>დღგ</legend>
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 170px;"><label for="payer">გადამხდელია</label></td>
							<td>
								<select id="payer" class="idls small">' . VatStatus($res[partn_VAT_sertifikati]) . '</select>
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="p_data">თარიღი</label></td>
							<td>
								<input type="text" id="p_data" class="idle date" onblur="this.className=\'idle date\'" onfocus="this.className=\'activeField date\'" value="' . $res['vat_data'] . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="ps_number">სერთიფიკატის ნომერი</label></td>
							<td>
								<input type="text" id="ps_number" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['vat_num'] . '" />
							</td>
						</tr>
					</table>
			    </fieldset>
			    <!-- ID -->
				<input type="hidden" id="part_id" value="' . $res['id'] . '" />
			</div>
			<!-- /Main Info -->
			<!-- Account -->
			<div id="tabs-2">
				<fieldset>		
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
									<button id="add_acc_button">დამატება</button>
					        	</div>
					            <div id="dynamic">						
									<table class="display" id="acc_details">
					                    <thead>
					                        <tr id="datatable_header">
					                            <th>ID</th>
					                            <th style="width: 25%">ოპერაციის თარიღი</th>
					                            <th style="width: 25%">ზედნადები</th>
					                            <th style="width: 25%">სულ გადასახდელი</th>
					                            <th style="width: 25%">გადახდილი</th>
					                        </tr>
					                    </thead>
					                    <thead>
					                        <tr class="search_header">
					                            <th class="colum_hidden">
					                            	<input type="text" name="search_a_id" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_date" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_overhead" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_branch" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_code" value="ფილტრი" class="search_init" />
					                            </th>
					                        </tr>
					                    </thead>
					                </table>						
					            </div>
					            <div id="bottom_button_area">
					        		<button id="export_acc_button" class="right">ექსპორტი</button>
					        	</div>
					        </div>
					    </div>
					</div>
				</fieldset>
				
				<!-- jQuery Dialog -->
			    <div id="add-edit-acc-form" class="form-dialog" title="ანგარიშწორება">
			    	<!-- aJax -->
				</div>
			</div>
			<!-- /Account -->
			<!-- Bank -->
			<div id="tabs-3">
				<fieldset>		
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
									<button id="add_bank_button">დამატება</button><button id="delete_bank_button">წაშლა</button>
					        	</div>
					            <div id="dynamic">						
									<table class="display" id="bank_details">
					                    <thead>
					                        <tr id="datatable_header">
					                            <th>ID</th>
					                            <th style="width: 25%">დასახელება</th>
					                            <th style="width: 25%">ფილიალი</th>
					                            <th style="width: 25%">ბანკის კოდი</th>
					                            <th style="width: 25%">ანგარიშის №</th>
					                            <th class="check">#</th>
					                        </tr>
					                    </thead>
					                    <thead>
					                        <tr class="search_header">
					                            <th class="colum_hidden">
					                            	<input type="text" name="search_b_id" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_bank_name" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_branch" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_code" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_account" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                            	<input type="checkbox" name="check-all-bank" id="check-all-bank">
					                            </th>
					                        </tr>
					                    </thead>
					                </table>						
					            </div>
					            <div id="bottom_button_area">
					        		<button id="export_bank_button" class="right">ექსპორტი</button>
					        	</div>
					        </div>
					    </div>
					</div>
				</fieldset>
				
				<!-- jQuery Dialog -->
			    <div id="add-edit-bank-form" class="form-dialog" title="საბანკო რეკვიზიტები">
			    	<!-- aJax -->
				</div>
			</div>
			<!-- /Bank -->
			<!-- Cadre -->
			<div id="tabs-4">
				<fieldset>
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
									<button id="add_cadre_button">დამატება</button><button id="delete_cadre_button">წაშლა</button>
					        	</div>
					            <div id="dynamic">						
									<table class="display" id="cadre_details">
					                    <thead>
					                        <tr id="datatable_header">
					                            <th>ID</th>
					                            <th style="width: 25%">პირადი ნომერი</th>
					                            <th style="width: 25%">სახელი</th>
					                            <th style="width: 25%">გვარი</th>
					                            <th style="width: 25%">თანამდებობა</th>
					                            <th class="check">#</th>
					                        </tr>
					                    </thead>
					                    <thead>
					                        <tr class="search_header">
					                            <th class="colum_hidden">
					                            	<input type="text" name="search_c_id" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_user_id" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_f_name" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_l_name" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_position" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                            	<input type="checkbox" name="check-all-cadre" id="check-all-cadre">
					                            </th>
					                        </tr>
					                    </thead>
					                </table>						
					            </div>
					            <div id="bottom_button_area">
					        		<button id="export_cadre_button" class="right">ექსპორტი</button>
					        	</div>
					        </div>
					    </div>
					</div>
				</fieldset>
			        
				<!-- jQuery Dialog -->
			    <div id="add-edit-cadre-form" class="form-dialog" title="კადრები">
			    	<!-- aJax -->
				</div>
			</div>
			<!-- /Cadre -->						
		</div>
    </div>
    ';
	return $data;
}
?>